//
//  ReplyListViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/4/19.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ReplyListViewController.h"
#import "PrivateLetterMsgViewController.h"
#import "LeavewordMsgViewController.h"
#import "CommentMsgDetailViewController.h"
#import "MessageCell.h"
#import "CommentNoticeModel.h"
#import "PraviteRecieveModel.h"

@interface ReplyListViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSMutableArray *_msgReplyListArray;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSString *msgType;
@end

@implementation ReplyListViewController

#pragma mark -- request
//私信回复列表
- (void)praviteRecieveListRequest {
    NSString *userType = KString([[LSUserInfoManager sharedManager] currentUserInfo].u_type);
    NSDictionary *dict;
    if ([userType intValue] == 100) {//明星
        dict = @{@"udid":User_ID,
                 @"token":TOKEN,
                 @"page":@"1",
                 @"status":@"",
                 @"star":userType
                 };
    } else { //普通
        dict = @{@"udid":User_ID,
                 @"token":TOKEN,
                 @"page":@"1",
                 @"status":@"",
                 };
    }
    
    NSString *urlString = [TEST_URL stringByAppendingString:kPraviteList];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *dic in array) {
                PraviteRecieveModel *praviteModel = [[PraviteRecieveModel alloc] initWithDict:dic];
                [_msgReplyListArray addObject:praviteModel];
            }
            [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

//留言 评论 回复列表请求
- (void)MsgListRequest {
    NSString *userType = KString([[LSUserInfoManager sharedManager] currentUserInfo].u_type);
    NSString *star;
    if ([userType isEqualToString:@"100"]) {
        star = User_ID;
    } else {
        star = @"";
    }
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           @"type":self.msgType,
                           @"page":@"1",
                           @"star":star
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kTrendReplyList];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *msgDict in array) {
                CommentNoticeModel *model = [[CommentNoticeModel alloc] initWithDict:msgDict];
                [_msgReplyListArray addObject:model];
            }
            [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    [self setUpUI];
}

- (void)setNav {
    switch (self.replyType) {
        case ReplyType_Private:{//私信
            self.navigationItem.title = @"私信回复列表";
            self.msgType = @"40";
            [self praviteRecieveListRequest];
        }
            break;
        case ReplyType_Leaveword:{//留言
            self.navigationItem.title = @"留言回复列表";
            self.msgType = @"30";
            [self MsgListRequest];
        }
            break;
        case ReplyType_Comment:{//评论
            self.navigationItem.title = @"评论回复列表";
            self.msgType = @"20";
            [self MsgListRequest];
        }
            break;
            
        default:
            break;
    }

}
#pragma mark -- UI
- (void)setUpUI {
    _msgReplyListArray = [NSMutableArray arrayWithCapacity:1];
    [self creatTableView];
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, kSCREEN_WIDTH, kSCREEN_HEIGHT- kNAV_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _msgReplyListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MessageCell *cell = [MessageCell loadNibCellWithTabelView:tableView];
    if ([self.msgType isEqualToString:@"40"]) {//私信
        PraviteRecieveModel *model = _msgReplyListArray[indexPath.row];
        cell.praviteModel = model;
    } else {
        CommentNoticeModel *model = _msgReplyListArray[indexPath.row];
        cell.commentnoticeModel = model;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (self.replyType) {
        case ReplyType_Private:{//私信
            PrivateLetterMsgViewController *privateVC = [[PrivateLetterMsgViewController alloc] init];
            PraviteRecieveModel *model = _msgReplyListArray[indexPath.row];
            privateVC.praviteModel = model;
            [self.navigationController pushViewController:privateVC animated:YES];
        }
            break;
        case ReplyType_Leaveword:{//留言
            LeavewordMsgViewController *leavewordVC = [[LeavewordMsgViewController alloc] init];
            CommentNoticeModel *model = _msgReplyListArray[indexPath.row];
            leavewordVC.commentNoticeModel = model;
            [self.navigationController pushViewController:leavewordVC animated:YES];
        }
            break;
        case ReplyType_Comment:{//评论
            CommentMsgDetailViewController *commentVC = [[CommentMsgDetailViewController alloc] init];
            CommentNoticeModel *model = _msgReplyListArray[indexPath.row];
            commentVC.commentNoticeModel = model;
            [self.navigationController pushViewController:commentVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
